package com.gem.listener; /**
 * @author 刘聪
 * Created  on 2020/10/9
 */

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class ApplicationListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {
//    当容器启动并且初始化时将会自动调用该方法

    @Override
    public void contextInitialized(ServletContextEvent sce) {
//         获取容器对象
        ServletContext servletContext = sce.getServletContext();
//        获取系统应用路径
        String base = sce.getServletContext().getContextPath();
//        把应用路径存入容器对象
        servletContext.setAttribute("base", base);
    }

    //当容器关闭时,自动调用该方法
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        servletContext.removeAttribute("base");
    }

}
